$(function() {

    // placeholder for IE
    $(".pay-wrap input[type=text]").placeholder();

    // custom select
    $(".query-select select").customSelect();

    // 输入充值账号
    var account = $(".input-account");
    var accountInit = $.trim(account.val());

    if (accountInit) {
        account.each(function() {
            if ($(this).is(':visible')) $(this).trigger("blur");
        });
    } else {
        account.each(function() {
            if ($(this).is(':visible')) $(this).trigger("input");
        });
    }
    account.on("blur", function() {
        $(this).parent().removeClass("active");
    });
    if (Cookies.get("pay-account") !== "undefined" && Cookies.get("pay-account") != "") {
        account.each(function() {
            $(this).val(Cookies.get("pay-account"));
        });
    }
    account.on("focus", function() {
        $(this).parent().addClass("active");
    });

    // 自定义扫码充值 - 输入其他金额
    var amountQrcode = $(".input-amount-qrcode");
    var qrcodeInit = Cookies.get("amount-qrcode");
    if (qrcodeInit === "undefined" || isNaN(qrcodeInit)) {
        var labelInit = $(".rating label:first");
        var val = parseInt($.trim(amountQrcode.val()));
        var label = amountQrcode.closest(".label-item").find("label");
        qrcodeInit = parseInt(labelInit.text());
        amountQrcode.val(qrcodeInit);
        amountQrcode.attr("fee", qrcodeInit);
        label.removeClass("active");
        label.each(function() {
            if (parseInt($(this).text()) == val) {
                $(this).addClass("active").siblings().removeClass("active");
            }
        });
        labelInit.click();
    }
    Cookies.set("amount-qrcode", qrcodeInit);
    /* if (qrcodeInit !== "undefined") {
    	amountQrcode.val(qrcodeInit);
    	amountQrcode.attr("fee", qrcodeInit);
        var val = parseInt($.trim(amountQrcode.val()));
        var label = amountQrcode.closest(".label-item").find("label");
        label.removeClass("active");
        label.each(function(){
            if (parseInt($(this).text()) == val) {
                $(this).addClass("active").siblings().removeClass("active");
            }
        });
    }
    if (isNaN(qrcodeInit)) {
        var labelInit = $(".rating label:first");
        Cookies.set("amount-qrcode", parseInt(labelInit.text()));
        labelInit.click();
    } */

    // 是否显示折扣提示
    function amountLimit(el) {
        var val = $.trim(el.val());
        var closest = el.closest(".pay-panel");
        if (val < 400) {
            closest.find(".discount").hide();
        } else {
            closest.find(".discount").show();
        }
        if (!val || isNaN(val)) {
            el.val("");
        }
    }
    amountLimit(amountQrcode);
    payInduce(amountQrcode);

    amountQrcode.on("blur", function() {
        $(this).parent().removeClass("active");
        var val = parseInt($.trim(this.value));
        if (val === "NaN" || val === "") {
            return false;
        }

        var min = $(this).data("min");
        var max = $(this).data("max");

        val = Math.max(val, min);
        val = Math.min(val, max);
        if (val >= min) {
            $(this).val(val).attr("fee", val);
        }
        if ((typeof subid !== "undefined") && subid == "zzb") {
            gamepoint_convert_zzb();
        } else {
            checkAmount($(this));
            payInduce($(this));
            updateFee($(this));
        }
    });
    amountQrcode.val(qrcodeInit).attr("fee", qrcodeInit).trigger("blur");

    amountQrcode.on("focus", function() {
        var closest = $(this).closest(".pay-panel");
        $(this).parent().addClass("active");
        $(this).closest(".label-item").find("label").removeClass("active");
        var val = $.trim(this.value);
        if (isNaN(val)) {
            $(this).val("1");
            if (window._GPConf) {
                var num = closest.find(".num span i");
            } else {
                var num = closest.find(".num span");
            }
            num.text(val);
            var optionNum = closest.find(".label-option span");
            optionNum.text("1");
        }
        if (!val) {
            $(this).attr("fee", 0);
        }
        amountLimit(amountQrcode);
        if ((typeof subid !== "undefined") && subid == "zzb") {
            gamepoint_convert_zzb();
        } else {
            updateFee(amountQrcode);
        }
    });

    amountQrcode.on("keyup", function() {
        var val = parseInt($.trim(this.value));
        if (val === "NaN" || val === "") {
            return false;
        }

        var panel = $(".pay-panel");
        var min = $(this).data("min");
        var max = $(this).data("max");

        val = Math.max(val, min);
        val = Math.min(val, max);
        if (val >= min) {
            $(this).val(val).attr("fee", val);
        }

        if (window._GPConf) {
            var num = panel.find(".num span i");
        } else {
            var num = panel.find(".num span");
        }
        num.text(val);
        var optionNum = panel.find(".label-option span");
        optionNum.text(val);
        amountLimit(amountQrcode);

        if ((typeof subid !== "undefined") && subid == "zzb") {
            gamepoint_convert_zzb();
        } else {
            payInduce(amountQrcode);
            updateFee(amountQrcode);
        }
    });

    // 输入验证码
    var captcha = $(".input-captcha");
    captcha.on("blur", function() {
        checkCaptcha($(this));
        $(this).parent().removeClass("active");
    });
    captcha.on("focus", function() {
        $(this).parent().addClass("active");
    });

    // 输入一卡通卡号
    var ocnum = $(".input-ocnum");
    ocnum.on("blur", function() {
        checkOcnum($(this));
        $(this).parent().removeClass("active");
    });
    ocnum.on("focus", function() {
        $(this).parent().addClass("active");
    });
    ocnum.numFormat();

    // 输入一卡通密码
    var ocpwd = $(".input-ocpwd");
    ocpwd.on("blur", function() {
        checkOcpwd($(this));
        $(this).parent().removeClass("active");
    });
    ocpwd.on("focus", function() {
        $(this).parent().addClass("active");
    });
    ocpwd.numFormat();

    // 输入手机号
    var mobile = $(".input-mobile");
    mobile.on("blur", function() {
        var el = $(this);
        var closest = el.closest(".label-item");
        v = $(this).val();
        var isError = !isMobile.test(v);
        closest.find(".form-tips").remove();
        if (isError) {
            el.parent().addClass("has-error");
            $("<p class='form-tips'><i class='zticon'>&#xe622;</i>" + tipsLang.mobileErr + "</p>").appendTo(closest);
            // 输入为空时提示
            if ($.trim(el.val()) == "") {
                closest.find(".form-tips").remove();
                $("<p class='form-tips'><i class='zticon'>&#xe622;</i>" + tipsLang.mobileMiss + "</p>").appendTo(closest);
            }
            return false;
        } else {
            el.parent().removeClass("has-error");
        }
        $(this).parent().removeClass("active");
    });
    mobile.on("focus", function() {
        $(this).parent().addClass("active");
    });
    mobile.mobileFormat();

    // 输入充值卡卡号
    var fillNum = $(".input-fillnum");
    fillNum.on("blur", function() {
        checkFillnum($(this));
        $(this).parent().removeClass("active");
    });
    fillNum.on("focus", function() {
        $(this).parent().addClass("active");
    });
    fillNum.numFormat();

    // 输入充值卡密码
    var fillPwd = $(".input-fillpwd");
    fillPwd.on("blur", function() {
        checkFillpwd($(this));
        $(this).parent().removeClass("active");
    });
    fillPwd.on("focus", function() {
        $(this).parent().addClass("active");
    });
    fillPwd.numFormat();

    // 一卡通 | 手机充值卡输入
    $(".input-ocnum, .input-ocpwd, .input-fillnum, .input-fillpwd").on("keyup", function() {
        var val = $.trim(this.value);
        var emptyFields = $(this).filter(function() {
            return val === "";
        });
        val.filterNum(this);
    });

    // input auto focus
    var inp = $("input[type=text]");
    $.each(inp, function() {
        if ($.trim(this.value)) {
            $(this).addClass("nofocus");
        }
    });

    // 光标 focus 到首个空输入框内
    $(".pay-panel input[type=text]:not(.nofocus):first").trigger("focus");

    if ((typeof subid !== "undefined") && subid == "zzb") {
        eventOfInputMoney(".input-amount-qrcode", gamepoint_convert_zzb);
    } else {
        if (typeof gamepoint_convert1 !== "undefined") {
            GamePointLoading();
            eventOfInputMoney(".input-amount-qrcode", gamepoint_convert1);
        } else {
            eventOfInputMoney(".input-amount-qrcode");
        }
    }

})